﻿using Engine;
using Game;

namespace Mekiasm.Common
{
    public class ElectricElement : Element<Interface.IMekPowerMachine>
    {
        public ElectricElement(int x, int y, int z) : base(x, y, z)
        {
        }

        public void IncreasePower()
        {
            for (int i = 0; i < Connections.Count; i++)
            {
                var connect = Connections[i];
                Point3 point = connect.Point - Point;
                int thisF = Point3ToFace(point);
                int face = OppositeFace(thisF);
                var neiborMachine = connect.Instance.Machine;
                var currentMahcine = Instance.Machine;
                connect.Instance.Machine.MachineFaceConfig.TryGetValue(face, out MachineFaceConfig config);
                Instance.Machine.MachineFaceConfig.TryGetValue(thisF, out MachineFaceConfig configF);
                if (config.DeviceType == MekBlock.DeviceType.Electric && config.MachineMode == MekBlock.MachineMode.OUT && configF.DeviceType == MekBlock.DeviceType.Electric && configF.MachineMode == MekBlock.MachineMode.IN)
                {
                    if (neiborMachine.Power >= currentMahcine.InputPowerLimit && neiborMachine.OutputPowerLimit >= currentMahcine.InputPowerLimit)
                    {
                        if (currentMahcine.Power + currentMahcine.InputPowerLimit <= currentMahcine.MaxPower)
                        {
                            currentMahcine.Power += currentMahcine.InputPowerLimit;
                            neiborMachine.Power -= currentMahcine.InputPowerLimit;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 格子物品对机器充电或者放电
        /// </summary>
        /// <param name="chargeSlot">格子位置</param>
        public void ChargeOrDischargeItem(int chargeSlot)
        {
            var currentMachine = Instance.Machine;
            var slot = currentMachine.Slots[chargeSlot];
            int value = slot.Value;
            var block = BlocksManager.Blocks[Terrain.ExtractContents(value)];
            //默认SlotIn
            bool SlotOutputMachine = slot.SlotType.HasFlag(ComponentEnergyMachine.SlotType.Charge_SlotOut);
            if (slot.Count > 0 && block is MekBlock mek)
            {
                if (mek.GetItem(ref value) is Interface.IChargeAbleItem charge && charge.ChargeFuelType == ComponentEnergyMachine.FuelType.Electric)
                {
                    if (SlotOutputMachine)
                    {
                        if (charge.GetCurrentAmount(value) >= currentMachine.InputPowerLimit && currentMachine.CanAcceptFuel(chargeSlot, 0) && currentMachine.Power + currentMachine.InputPowerLimit <= currentMachine.MaxPower)
                        {
                            slot.Value =  charge.SetCurrentAmount(value,0, -currentMachine.InputPowerLimit);
                            currentMachine.Power += currentMachine.InputPowerLimit;
                        }
                    }
                    else
                    {
                        if (currentMachine.Power >= currentMachine.OutputPowerLimit && charge.GetCurrentAmount(value) + currentMachine.OutputPowerLimit <= charge.MaxAmount)
                        {
                            slot.Value = charge.SetCurrentAmount(value,0, currentMachine.OutputPowerLimit);
                            currentMachine.Power -= currentMachine.OutputPowerLimit;
                        }
                    }
                }
            }
        }
    }
}
